﻿'//DotNetNuke - http://www.dotnetnuke.com
'// Copyright (c) 2002-2010
'// by DotNetNuke Corporation
'//
'// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
'// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
'// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
'// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'//
'// The above copyright notice and this permission notice shall be included in all copies or substantial portions
'// of the Software.
'//
'// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
'// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
'// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
'// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
'// DEALINGS IN THE SOFTWARE.


Imports DotNetNuke.Entities.Modules
Imports DotNetNuke.Services.Search
Imports System.Xml

Namespace Components

    Public Class FeatureController
        Implements IPortable
        Implements ISearchable

        '/// -----------------------------------------------------------------------------
        '/// <summary>
        '/// ExportModule implements the IPortable ExportModule Interface
        '/// </summary>
        '/// <param name="ModuleID">The Id of the module to be exported</param>
        '/// -----------------------------------------------------------------------------
        Public Function ExportModule(ByVal ModuleID As Integer) As String Implements IPortable.ExportModule
            Dim strXML As String = ""

            Dim coldnnsimplearticles As List(Of Article) = ArticleController.GetArticles(ModuleID)
            If coldnnsimplearticles.Count <> 0 Then
                strXML += "<articles>"

                For Each objArticle As Article In coldnnsimplearticles
                    strXML += "<article>"
                    strXML += "<title>" + Common.Utilities.XmlUtils.XMLEncode(objArticle.Title) + "</title>"
                    strXML += "<description>" + Common.Utilities.XmlUtils.XMLEncode(objArticle.Description) + "</description>"
                    strXML += "<body>" + Common.Utilities.XmlUtils.XMLEncode(objArticle.Body) + "</body>"
                    strXML += "</article>"
                Next
                strXML += "</articles>"
            End If

            Return strXML

        End Function

        '/// -----------------------------------------------------------------------------
        '/// <summary>
        '/// ImportModule implements the IPortable ImportModule Interface
        '/// </summary>
        '/// <param name="ModuleID">The Id of the module to be imported</param>
        '/// <param name="Content">The content to be imported</param>
        '/// <param name="Version">The version of the module to be imported</param>
        '/// <param name="UserId">The Id of the user performing the import</param>
        '/// -----------------------------------------------------------------------------
        Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserID As Integer) Implements IPortable.ImportModule
            Dim mc As New ModuleController()
            Dim mi = mc.GetModule(ModuleID)

            Dim xmldnnsimplearticles As XmlNode = DotNetNuke.Common.Globals.GetContent(Content, "articles")

            For Each xmldnnsimplearticle As XmlNode In xmldnnsimplearticles.SelectNodes("article")
                Dim objdnnsimplearticle As New Article()
                objdnnsimplearticle.ModuleId = ModuleID
                objdnnsimplearticle.Title = xmldnnsimplearticle.SelectSingleNode("title").InnerText
                objdnnsimplearticle.Description = xmldnnsimplearticle.SelectSingleNode("description").InnerText
                objdnnsimplearticle.Body = xmldnnsimplearticle.SelectSingleNode("body").InnerText
                objdnnsimplearticle.CreatedByUserId = UserID
                objdnnsimplearticle.CreatedOnDate = DateTime.Now
                objdnnsimplearticle.LastModifiedByUserId = UserID
                objdnnsimplearticle.LastModifiedOnDate = DateTime.Now
                objdnnsimplearticle.Save(mi.TabID)
            Next

        End Sub

        '/// -----------------------------------------------------------------------------
        '/// <summary>
        '/// GetSearchItems implements the ISearchable Interface
        '/// </summary>
        '/// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param>
        '/// -----------------------------------------------------------------------------
        Public Function GetSearchItems(ByVal ModInfo As ModuleInfo) As SearchItemInfoCollection Implements ISearchable.GetSearchItems
            Dim searchItemCollection = New SearchItemInfoCollection()

            Dim colArticles As List(Of Article) = ArticleController.GetArticles(ModInfo.ModuleID)

            For Each objArticle As Article In colArticles
                Dim searchItem As New SearchItemInfo
                searchItem.Title = objArticle.Title
                searchItem.Description = objArticle.Description
                searchItem.Author = objArticle.CreatedByUserId
                searchItem.PubDate = objArticle.LastModifiedOnDate
                searchItem.ModuleId = ModInfo.ModuleID
                searchItem.SearchKey = objArticle.ArticleId.ToString()
                searchItem.Content = objArticle.Body
                searchItem.GUID = "aid=" + objArticle.ArticleId.ToString
                searchItemCollection.Add(searchItem)
            Next

            Return searchItemCollection
        End Function
    End Class

End Namespace
